class Solution {
public:
    int findMin(vector<int>& nums) {
        int left = 0;
        int right = nums.size() - 1;
        while (left < right)
        {
            int  mid = left + (right - left) / 2;
            if (nums[mid] > nums[nums.size() - 1])
                left = mid + 1;
            else
                right = mid;






        }
        return nums[left];
    }
}; class Solution {
public:
    vector<int> searchRange(vector<int>& nums, int target) {
        int left = 0;
        int right = nums.size() - 1;
        if (nums.size() == 0)
            return { -1,-1 };
        while (left < right)
        {
            int mid = left + (right - left) / 2;
            if (nums[mid] < target)
                left = mid + 1;
            else
                right = mid;
        }

        int begin = left;
        if (nums[left] != target)
            begin = -1;
        left = 0; right = nums.size() - 1;
        while (left < right)
        {
            int mid = left + (right - left + 1) / 2;
            if (nums[mid] <= target)
                left = mid;
            else
                right = mid - 1;







        }
        int end = left;
        if (nums[left] != target)
            end = -1;
        return { begin,end };



    }
}; class Solution {
public:
    int searchInsert(vector<int>& nums, int target) {
        int left = 0;
        int right = nums.size() - 1;
        while (left < right)
        {
            int mid = left + (right - left) / 2;
            if (nums[mid] < target)
                left = mid + 1;
            else
                right = mid;

        }
        if (nums[left] < target)
            return left + 1;
        return left;







    }
}; class Solution {
public:
    bool searchMatrix(vector<vector<int>>& matrix, int target) {

        int m = matrix.size() - 1;
        int n = 0;
        while (m >= 0 && n < matrix[0].size())
        {
            if (matrix[m][n] > target)
                m--;
            else if (matrix[m][n] < target)
                n++;
            else
                return true;













        }
        return false;







    }
}; class Solution {
public:
    int findDuplicate(vector<int>& nums) {
        int left = 1;
        int right = nums.size() - 1;
        while (left < right)
        {
            int mid = left + (right - left) / 2;

            int count = 0;
            for (auto e : nums)
            {
                if (e <= mid)
                {
                    count++;
                }


            }
            if (count > mid)
                right = mid;
            else
                left = mid + 1;







        }
        return right;
    }
};